﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.WebAPI.Models;

namespace ThresholdAssistant.WebAPI.Repository
{

    /// <summary>
    /// 阈值表仓储
    /// </summary>
    public class ThresholdRepository : BaseRepository, IBaseAutofac
    {
        /// <summary>
        /// 根据组ID查询列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Threshold>> GetListByGroupIdAsync(long groupId)
        {
            var sql = @"
                SELECT t.id, t.group_id, t.NAME, t.threshold_type, t.start_value
	                , t.end_value, t.create_id, t.create_time, t.update_time, t.remark
                FROM t_threshold t
                WHERE t.group_id = @GroupId";

            return await mydb.GetListAsync<Threshold>(sql, new { GroupId = groupId });
        }


        /// <summary>
        /// 新增
        /// </summary>
        public async Task<long> AddAsync(Threshold model)
        {
            string insertSql = @"
                INSERT INTO `t_threshold`
                    (`group_id`, `name`, `threshold_type`, `start_value`, `end_value`,`create_id`, `create_time`, `update_time`, `remark`)
                VALUES
                    (@GroupId, @Name, @ThresholdType, @StartValue, @EndValue, @CreateId, @CreateTime, @UpdateTime, @Remark);
                SELECT @@IDENTITY;";
            return await mydb.ExecuteScalarAsync<long>(insertSql, model);
        }

        /// <summary>
        /// 单个
        /// </summary>
        public async Task<Threshold> GetInfoAsync(long id)
        {
            var sql = @"
                SELECT t.id, t.group_id, t.NAME, t.threshold_type, t.start_value
	                , t.end_value, t.create_id, t.create_time, t.update_time, t.remark
                FROM t_threshold t
                WHERE t.id = @id";

            return await mydb.GetFirstOrDefaultAsync<Threshold>(sql, new { id });

            //var list = await mydb.GetListAsync<Threshold>(sql, new { id });
            //if(list != null && list.Count() > 0)
            //    return list.ToList()[0];
            //return null;
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateAsync(Threshold model)
        {
            string updateSql = @"
                UPDATE t_threshold
                    SET `name` = @Name, `threshold_type` = @ThresholdType, `start_value` = @StartValue, `end_value` = @EndValue
                        , `update_time` = @UpdateTime
                WHERE `id` = @Id
            ";
            return await mydb.ExecuteAsync(updateSql, model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public async Task<bool> DeleteAsync(long id)
        {
            string deleteSql = @"DELETE FROM t_threshold WHERE id = @id;";
            return await mydb.ExecuteAsync(deleteSql, new { id });
        }
    }
}
